﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;

namespace eTerm.ASynClientSDK.Base {
    /// <summary>
    /// 分页指令基类
    /// </summary>
    public abstract class ASyncPNCommand:ASynCommand {

        private string m_PnCommand = @"PN";

        /// <summary>
        /// 下页指令.
        /// </summary>
        /// <value>The pn command.</value>
        protected virtual string PnCommand { get { return m_PnCommand; } set { m_PnCommand = value; } }

        /// <summary>
        /// 是否还有下页数据（将自动执行“PnCommand”）.
        /// </summary>
        /// <param name="msgBody">当前指令结果.</param>
        /// <returns></returns>
        /// <value><c>true</c> if [exist next page]; otherwise, <c>false</c>.</value>
        protected virtual bool ExistNextPage(string msgBody) {
            return false;
        }

        /// <summary>
        /// 最大分页次数(避免无限分页).
        /// </summary>
        /// <value>The max pn count.</value>
        protected virtual int? MaxPnCount { get; private set; }

        /// <summary>
        /// 生成指令并发送分析(子类必须重写).
        /// </summary>
        /// <param name="SynCmd">eTerm实质指令.</param>
        /// <returns></returns>
        protected override ASyncResult GetSyncResult(string SynCmd) {
            if(!IsAuthorized)
                Connect();
            string lastResult = string.Empty;
            StringBuilder pnResult = new StringBuilder();

            #region IG数据
            //int IgCount = 5;
            //bool flag = true;
            //while (IgCount-- > 0 && flag)
            //{
            //    SendStream(@"IG");
            //    flag = !Regex.IsMatch(ConvertResult(GetStream()), @"NO\s+PNR"); ;
            //}
            #endregion

            SendStream(SynCmd);
            string PnResult = ConvertResult(GetStream());
            pnResult.Append(PnResult);
            int maxPnCount = this.MaxPnCount??10;
            while (ExistNextPage(PnResult) && --maxPnCount>0) {
                //Thread.Sleep(int.Parse((this.AsnCommandSleep??0.5 * 1000).ToString()));
                ThreadSleep();
                SendStream(PnCommand);
                PnResult = ConvertResult(GetStream());
                lastResult = PnResult;
                //pnResult.AppendFormat("\r{0}", PnResult);
                AppendResult(pnResult, PnResult, SynCmd);
            }
            Dispose();
            ASyncResult Restult = ResultAdapter(pnResult.ToString());
#if DEBUG
            Restult.ASynCmd = SynCmd;
            Restult.ASyncInfo = lastResult;
            Restult.OrgCommandResult = pnResult.ToString();
#else 
            Restult.OrgCommandResult = pnResult.ToString();
#endif
            EndExecuteTimeSpan = new TimeSpan(DateTime.Now.Ticks); //获取当前时间的刻度数
            Restult.TotalSeconds = (TotalSeconds.TotalMilliseconds / 1000.0).ToString("f2");
            return Restult;
        }

        /// <summary>
        /// Appends the result.
        /// </summary>
        /// <param name="pnResult">The pn result.</param>
        /// <param name="PnResult">The pn result.</param>
        /// <param name="SynCmd">The syn CMD.</param>
        protected virtual void AppendResult(StringBuilder pnResult, string PnResult, string SynCmd) {
            pnResult.AppendFormat("\r{0}", PnResult);
        }
    }
}
